<!-- 护理项目列表页 -->
<template>
  <div class="min-h serveProject bg-wt">
    <SearchForm
      :searchData="pagination"
      @handleSearch="getList"
      @handleReset="handleReset"
      @handleClear="handleClear"
    ></SearchForm>
    <TabeList
      :data="data"
      :stripe="stripe"
      :bordered="bordered"
      :hover="hover"
      :size="size"
      :total="total"
      :pagination="pagination"
      @onPageChange="onPageChange"
      @handleBulid="handleBulid"
      @handleEdit="handleEdit"
      @handleClickDelete="handleClickDelete"
      @handleForbidden="handleForbidden"
    ></TabeList>
    <Dialogfrom
      ref="fromRef"
      :visible="visible"
      :data="fromDate"
      :minNumber="minNumber"
      :title="title"
      @handleClose="handleClose"
      @fetchData="fetchData"
      @handleAdd="handleAdd"
      @handleEdit="handleEditFrom"
    ></Dialogfrom>
  </div>
</template>

<script setup lang="jsx">
import { ref } from 'vue'
import TabeList from './components/TabeList.vue'
import { onMounted } from 'vue'
import {
  getProjectList,
  projectAdd,
  projectDelete,
  projectStatus
} from '@/api/serve'
import SearchForm from './components/SearchForm.vue'
import Dialogfrom from './components/DialogForm.vue'
import { MessagePlugin } from 'tdesign-vue-next'
import { getProjectDetails } from '@/api/serve'
import { projectUpdate } from '@/api/serve'

const data = ref([])
const total = ref(0)
const visible = ref(false)
const stripe = ref(true)
const bordered = ref(true)
const hover = ref(false)
const size = ref('medium')

//分页对象
const pagination = ref({
  pageSize: 5,
  pageNum: 1
})

// 翻页设置当前页
const onPageChange = (val) => {
  pagination.value.pageNum = val.current
  pagination.value.pageSize = val.pageSize
  getList()
}

const getList = async () => {
  let res = await getProjectList(pagination.value)
  data.value = res.data.records
  total.value = res.data.total
}

onMounted(() => {
  getList()
})

const handleReset = () => {
  pagination.value = {
    pageSize: 5,
    pageNum: 1
  }
  getList()
}

const fromRef = ref({})
const title = ref('')
const handleBulid = () => {
  title.value = '新增'
  visible.value = true
}

const handleClose = () => {
  visible.value = false
}

const handleAdd = async (val) => {
  let res = await projectAdd(val)
  if (res.code == 200) {
    MessagePlugin.success(res.msg)
    visible.value = false
    fromRef.value.handleClear()
    getList()
  } else {
    MessagePlugin.error(res.msg)
  }
}

const handleClear = (val) => {
  if (val == 'name') {
    delete pagination.value.name
  } else if (val == 'status') {
    delete pagination.value.status
  }
  getList()
}

//id查询
const fromDate = ref({})
const handleEdit = async (val) => {
  let res = await getProjectDetails(val)
  fromDate.value = res.data
  title.value = '修改'
  visible.value = true
}

//修改
const handleEditFrom = async (val) => {
  let res = await projectUpdate(val)
  if (res.code == 200) {
    MessagePlugin.success(res.msg)
    visible.value = false
    fromRef.value.handleClear()
    getList()
  } else {
    MessagePlugin.error(res.msg)
  }
}

const handleClickDelete = async (val) => {
  let res = await projectDelete(val)
  if (res.code == 200) {
    MessagePlugin.success('删除成功')
  } else {
    //MessagePlugin.error('删除失败，服务器忙')
  }
}

const handleForbidden = async (val) => {
  let res = await projectStatus(val)
  if (res.code == 200) {
    MessagePlugin.success(res.msg)
    visible.value = false
    fromRef.value.handleClear()
    getList()
  } else {
    MessagePlugin.error(res.msg)
  }
}
</script>
